<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label" style="display:none">id</label>
                    <div class="layui-input-block" style="display:none">
                        <input type="text" th:value="${user_id}" id="user_id" name="user_id" autocomplete="off"
                               placeholder="请输入id" class="layui-input">
                    </div>

                    <label class="layui-form-label">账号</label>
                    <div class="layui-input-block">
                        <input type="text" th:value="${user_name}" name="userName" lay-verify="required"
                               autocomplete="off" placeholder="请输入账号" class="layui-input">
                    </div>

                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">姓名</label>
                    <div class="layui-input-block">
                        <input type="text" th:value="${nick_name}" name="nickName" autocomplete="off"
                               placeholder="请输入姓名" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">邮箱</label>
                    <div class="layui-input-block">
                        <input type="text" th:value="${email}" name="email" autocomplete="off" placeholder="请输入邮箱"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">密码</label>
                    <div class="layui-input-block">
                        <input type="text" th:value="${password}" name="password" autocomplete="off" placeholder="请输入密码"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">电话</label>
                    <div class="layui-input-block">
                        <input type="text" th:value="${phone}" name="phone" autocomplete="off" placeholder="请输入电话"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">类型</label>
                    <div class="layui-input-block">
                        <input type="radio" name="status" value="true" title="激活"
                               th:checked="${ status == true}? 'true':'false'">
                        <input type="radio" name="status" value="false" title="禁用"
                               th:checked="${ status == false}? 'true':'false'">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        部门
                    </label>
                    <div class="layui-input-block">
                        <input type="number" id="deptId" th:value="${dept_id}" name="deptId" lay-verify="required"
                               style="display:none;width: 0px" autocomplete="off" class="layui-input">
                        <ul id="dataTree" class="dtree" data-id="0" th:data-value="${dept_id}"></ul>
                    </div>
                </div>
                <input type="text" id="roleId" name="roleId" style="display:none;"/>
                <div class="layui-form-item">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-block show-role-container">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['form', 'jquery', 'dtree'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let dtree = layui.dtree;
        // 初始化树
        dtree.render({
            elem: "#dataTree",
            initLevel: "1",//默认展开层级为1
            width: "100%",
            method: 'get',
            dataStyle: "layuiStyle",  //使用layui风格的数据格式
            // response:{message:"msg",statusCode:200},  //修改response中返回数据的定义
            url: "/dept/buildDeptAll",
            dataFormat: "list",  //配置data的风格为list
            select: true, //指定下拉树模式
            selectTips: "不选默认是顶级目录",
            selectCardHeight: "150"
        });
        dtree.on("node('dataTree')", function (obj) {
            var param = dtree.getNowParam("dataTree");
            $("#deptId").val(param.nodeId);
        });
        //获取所有的角色列表
        getAllRole(function () {
            layui.form.render('radio')
        });
        form.on('submit(user-save)', function (data) {
            var rolelRadio = $("[name=roleId]:checked");
            if (rolelRadio == undefined || rolelRadio.length <= 0) {
                layer.alert("请给该用户设置角色", {icon: 5});
                return false;
            }
            $.ajax({
                url: '/api/user/edit',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: "application/json;charset=utf-8",
                type: 'post',
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg(result.message, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("testReload");
                        });
                    } else {
                        layer.msg(result.message, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });

        //获取所有的角色列表
        function getAllRole(callback) {
            $.ajax({
                url: "/api/role/all",
                type: "GET",
                dataType: 'json',
                success: function (result) {
                    var html = "";
                    if (result.data && result.data.length > 0) {
                        for (i in result.data) {
                            html += '<input type="radio"  name="roleId" value="' + result.data[i].role_id + '" title="' + result.data[i].role_name + '" >' +
                                '<div class="layui-unselect layui-form-radio layui-form-radioed"><i class="layui-anim layui-icon"></i><div>' + result.data[i].role_name + '</div></div>';
                        }
                        $(".show-role-container").html(html);
                        getCurrentUserRole(callback);
                    }
                }
            });
        }

        //当前用户关联的角色id
        function getCurrentUserRole(callback) {
            var userId = $("#user_id").val();
            $.ajax({
                url: "/api/roleuser",
                type: "POST",
                data: JSON.stringify({
                    "userId": userId
                }),
                contentType: "application/json;charset=utf-8",
                dataType: 'json',
                success: function (result) {
                    if (result.code == 200) {
                        $("[name=roleId]").each(function () {
                            if ($(this).val() == result.data.roleId) {
                                $(this).attr("checked", "checked");
                                return false;
                            }
                        })
                    } else {
                        layer.alert(result.message)
                    }
                    if (callback) {
                        callback();
                    }
                }
            });
        }

    })
</script>
<script>
</script>
</body>
</html>